/**
 * @program: LeetCode
 * @description: LeetCode : N 皇后问题
 * @author: WXY
 * @create: 2023-01-06 20:01
 * @Version 1.0
 **/
public class NQueuePro {
    public static int num(int n) {
        if (n < 0) return 0;
        int[] record = new int[n];
        return process(0, record, n);
    }

    public static int process(int i, int[] record, int n) {
        if (i == n) return 1;
        int res = 0;
        for (int j = 0; j < n; j++) {
            if (isValid(record, i, j)) {
                record[i] = j;
                res += process(i + 1, record, n);
            }
        }
        return res;
    }

    private static boolean isValid(int[] record, int i, int j) {
        for (int k = 0; k < i; k++) {
            if (j == record[k] || Math.abs(record[k] - j) == Math.abs(i - k)) {
                return false;
            }
        }
        return true;
    }


    public static void main(String[] args) {
        int n = 15;
        long start = System.currentTimeMillis();
        int num = num(n);
        long end = System.currentTimeMillis();
        System.out.println(num);
        System.out.println("cost time = " + (end - start));
    }
}
